package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import jdk.nashorn.internal.ir.BreakableNode;
import org.hibernate.validator.internal.util.IdentitySet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return 品牌信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        //查询
        Brand byId = brandService.getById(id);
        //转换
        return ResponseEntity.ok(new BrandDTO(byId));
    }

    /**
     * 根据id集合查询品牌
     * @param ids id集合
     * @return 品牌集合
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids ){
        //查询
        List<Brand> list = brandService.listByIds(ids);
        //转换
        return ResponseEntity.ok(BrandDTO.convertEntityList(list));
    }

    /**
     * 根据分类id查询品牌集合
     * @param id  分类id
     * @return 品牌集合
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryCategoryById(@RequestParam("id") Long id){
        return ResponseEntity.ok( brandService.queryBrandByCid(id));
    }

    /**
     * 分页查询
     * @param key 条件值
     * @param page 当前页
     * @param rows 每页显示
     * @return  当前页显示的数据
     */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryByPage(
            @RequestParam(value = "key" ,required = false) String key ,
            @RequestParam(value = "page" ,defaultValue = "1") Integer page ,
            @RequestParam(value = "rows" , defaultValue = "5") Integer rows){
        return ResponseEntity.ok(brandService. queryByPage(key,page,rows ));
    }

    /**
     * 新增品牌
     * @param brandDTO 新增品牌信息
     * @return  新增成功
     *
     */
    @PostMapping
    public ResponseEntity<Void> addBrand (BrandDTO brandDTO){
        //新增数据
        brandService.saveByBrand(brandDTO);
        //显示添加成功201，不会显数据
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param brandDTO  更新品牌信息
     * @return 无
     */
   @PutMapping
   public ResponseEntity<Void> updateBrand (BrandDTO brandDTO){
        //更新数据
        brandService.updateBrand(brandDTO);
        //显示更新成功201，不会回显
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        //删除品牌
        brandService.deleteBrandById(id);
        //显示删除成功201，不会回显
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
